home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gold Medal Software 2
/
Gold Medal Software Volume 2 (Gold Medal) (1994).iso
/
windows
/
win31
/
macsyma.arj
/
MACSDEMO.EXE
/
FUNCTION.OUT
< prev
next >
Wrap
Text File
|
1993-09-15
|
5KB
|
110 lines
(c1) /*
DEMONSTRATION OF USER-DEFINED FUNCTIONS IN MACSYMA
1. Defining and evaluating user-defined functions */
f(x):= x^2+y;
|$label(0,15,Times New Roman,$(d1$))f$paren(x)$hinge()$in( := )$sup(x,2)$in( + )y
(c2) f(2);
|$label(0,15,Times New Roman,$(d2$))y$hinge()$in( + )4
(c3) ev(f(2),y:7);
|$label(0,15,Times New Roman,$(d3$))11
(c4) f(x):= sin(x)^2+1;
|$label(0,15,Times New Roman,$(d4$))f$paren(x)$hinge()$in( := )$sup(sin,2)$paren(x)$in( + )1
(c5) f(x+1);
|$label(0,15,Times New Roman,$(d5$))$sup(sin,2)$paren(x$in( + )1)$hinge()$in( + )1
(c6) g(y,z):= f(z)+3*y;
|$label(0,15,Times New Roman,$(d6$))g$paren(y$ina($, )$hinge()z)$hinge()$in( := )f$paren(z)$in( + )3$in( )y
(c7) ev(g(2*y+z,-0.5),y:7);
|$label(0,15,Times New Roman,$(d7$))3$in( )$paren(z$in( + )14,$(,$))$hinge()$in( + )1.22985
(c8) h(n):= sum(i*x^i,i,0,n);
|$label(0,15,Times New Roman,$(d8$))h$paren(n)$hinge()$in( := )sum$paren(i$in( )$sup(x,i)$ina($, )$hinge()i$ina($, )$hinge()0$ina($, )$hinge()n)
(c9) /* Define a function from an expression. */
expr: sin(x+a)*(y-b)^2;
|$label(0,15,Times New Roman,$(d9$))sin$paren(x$in( + )a)$hinge()$in( )$sup($paren(y$in( - )b,$(,$)),2)
(c10) myfun: funmake(lambda,[[x,y], expr]);
|$label(0,15,Times New Roman,$(d10$))$greektext(l)$paren($paren(x$ina($, )$hinge()y,[,])$ina($, )$hinge()sin$paren(x$in( + )a)$in( )$sup($paren(y$in( - )b,$(,$)),2))
(c11) myfun(3,z);
|$label(0,15,Times New Roman,$(d11$))sin$paren(a$in( + )3)$hinge()$in( )$sup($paren(z$in( - )b,$(,$)),2)
(c12) /* 2. Obtain a list of all user-defined functions in the environment. */
functions;
|$label(0,15,Times New Roman,$(d12$))$open([)f$paren(x)$ina($, )$hinge()g$paren(y$ina($, )$hinge()z)$ina($, )$hinge()h$paren(n)$close(])
(c13) /* Subscripted functions and recursion relations */
t[n](x):= ratexpand(2*x*t[n-1](x)-t[n-2](x));
|$label(0,15,Times New Roman,$(d13$))$sub(t,n)$paren(x)$hinge()$in( := )ratexpand$paren(2$in( )x$in( )$sub(t,n- 1)$paren(x)$in( - )$sub(t,n- 2)$paren(x))
(c14) t[0](x):= 1$
(c15) t[1](x):= x$
(c16) t[4](y);
|$label(0,15,Times New Roman,$(d16$))8$in( )$sup(y,4)$hinge()$in( - )8$in( )$sup(y,2)$hinge()$in( + )1
(c17) /* 3. Subscripted functions versus functions of two arguments:
subscripted functions store all final or intermediate computed
values. Also, note the difference in evaluation of arguments. */
g[n](x):= sum(ev(x),i,n,n+2);
|$label(0,15,Times New Roman,$(d17$))$sub(g,n)$paren(x)$hinge()$in( := )sum$paren(ev$paren(x)$ina($, )$hinge()i$ina($, )$hinge()n$ina($, )$hinge()n$in( + )2)
(c18) h(n,x):= sum(ev(x),i,n,n+2);
|$label(0,15,Times New Roman,$(d18$))h$paren(n$ina($, )$hinge()x)$hinge()$in( := )sum$paren(ev$paren(x)$ina($, )$hinge()i$ina($, )$hinge()n$ina($, )$hinge()n$in( + )2)
(c19) g[2](i^2);
|$label(0,15,Times New Roman,$(d19$))3$hinge()$in( )$sup(i,2)
(c20) h(2,i^2);
|$label(0,15,Times New Roman,$(d20$))29
(c21) /* The function G(Y,Z) is still defined from an earlier command,
even after defining the array function G[N](X) . */
g(2,3);
|$label(0,15,Times New Roman,$(d21$))$sup(sin,2)$paren(3)$hinge()$in( + )7
(c22) /* The array function P[N](X) evaluates the derivative before
evaluating the argument X; the function Q(N,X) does it the other way. */
p[n](x):= ratsimp(1/(2^n*n!)*diff((x^2-1)^n,x,n));
|$label(0,15,Times New Roman,$(d22$))$sub(p,n)$paren(x)$hinge()$in( := )ratsimp$paren($q(diff$paren($sup($paren($sup(x,2)$in( - )1,$(,$)),n)$ina($, )$hinge()x$ina($, )$hinge()n),$sup(2,n)$in( )n!))
(c23) q(n,x):= ratsimp(1/(2^n*n!)*diff((x^2-1)^n,x,n));
|$label(0,15,Times New Roman,$(d23$))q$paren(n$ina($, )$hinge()x)$hinge()$in( := )ratsimp$paren($q(diff$paren($sup($paren($sup(x,2)$in( - )1,$(,$)),n)$ina($, )$hinge()x$ina($, )$hinge()n),$sup(2,n)$in( )n!))
(c24) p[2];
C:\MACSD2B\library1\binoml.fas being loaded.
|$label(0,15,Times New Roman,$(d24$))$greektext(l)$paren($paren(x,[,])$ina($, )$hinge()$q(3$in( )$sup(x,2)$in( - )1,2))
(c25) p[2](y+1);
|$label(0,15,Times New Roman,$(d25$))$q(3$in( )$sup($paren(y$in( + )1,$(,$)),2)$in( - )1,2)
(c26) errcatch(q(2,y+1));
|$label(-1,15,Times New Roman,)y$in( + )1
Non-variable 2nd argument to DIFF:
|$label(-1,15,Times New Roman,)y$in( + )1
|$label(0,15,Times New Roman,$(d26$))$open([)$close(])
(c27) p[2](5);
|$label(0,15,Times New Roman,$(d27$))37
(c28) f[i,j](x,y):= x^i+y^j;
|$label(0,15,Times New Roman,$(d28$))$sub(f,i$ina($, )$hinge()j)$paren(x$ina($, )$hinge()y)$hinge()$in( := )$sup(x,i)$in( + )$sup(y,j)
(c29) g(fun,a,b):= print(fun," applied to ",a," and ",b," is ",fun(a,b))$
(c30) g(f[2,1],sin(%pi),2*c);
|$label(-1,15,Times New Roman,)$greektext(l)$paren($paren(x$ina($, )$hinge()y,[,])$ina($, )$hinge()y$in( + )$sup(x,2))$in() $in() applied to $in() $in()0$in() $in() and $in() $in()2$in( )c$in() $in() is $in() $in()2$in( )c
|$label(0,15,Times New Roman,$(d30$))2$hinge()$in( )c
(c31) /* Clean up */
remove([f,g,h,myfun,q], function, [f,g,p,t], array,[expr,myfun],value)$